package com.woniuxy.flowable.controller;

import com.woniuxy.flowable.dto.ResponseData;
import com.woniuxy.flowable.service.CustomerFlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户审批流程控制器
 * @author woniuxy
 */
@RestController
@RequestMapping("/customer-workflow")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class CustomerFlowableController {
    
    @Autowired
    private CustomerFlowableService customerFlowableService;
    
    /**
     * 启动客户审批流程
     */
    @PostMapping("/process/start")
    public ResponseData<?> startCustomerProcess(@RequestBody Map<String, Object> params) {
        try {
            String processDefinitionKey = (String) params.get("processDefinitionKey");
            String businessKey = (String) params.get("businessKey");
            @SuppressWarnings("unchecked")
            Map<String, Object> variables = (Map<String, Object>) params.get("variables");
            
            // 启动流程实例
            String processInstanceId = customerFlowableService.startCustomerProcess(processDefinitionKey, businessKey, variables);
            
            Map<String, Object> result = new HashMap<>();
            result.put("processInstanceId", processInstanceId);
            result.put("processDefinitionKey", processDefinitionKey);
            result.put("businessKey", businessKey);
            
            return ResponseData.success(result);
            
        } catch (Exception e) {
            return ResponseData.error("启动客户审批流程失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询客户审批待办任务
     */
    @GetMapping("/tasks/pending")
    public ResponseData<?> getPendingTasks(@RequestParam String userName) {
        try {
            List<Map<String, Object>> tasks = customerFlowableService.getPendingTasks(userName);
            return ResponseData.success(tasks);
        } catch (Exception e) {
            return ResponseData.error("查询待办任务失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询客户审批已办任务
     */
    @GetMapping("/tasks/completed")
    public ResponseData<?> getCompletedTasks(@RequestParam String userName) {
        try {
            List<Map<String, Object>> tasks = customerFlowableService.getCompletedTasks(userName);
            return ResponseData.success(tasks);
        } catch (Exception e) {
            return ResponseData.error("查询已办任务失败: " + e.getMessage());
        }
    }
    
    /**
     * 完成任务
     */
    @PostMapping("/tasks/complete")
    public ResponseData<?> completeTask(@RequestBody Map<String, Object> params) {
        try {
            String taskId = (String) params.get("taskId");
            @SuppressWarnings("unchecked")
            Map<String, Object> variables = (Map<String, Object>) params.get("variables");
            
            customerFlowableService.completeTask(taskId, variables);
            return ResponseData.success("任务完成成功");
            
        } catch (Exception e) {
            return ResponseData.error("完成任务失败: " + e.getMessage());
        }
    }
    
    /**
     * 通过审批
     */
    @PostMapping("/tasks/approve")
    public ResponseData<?> approveTask(@RequestParam("processInstanceId") String processInstanceId) {
        try {
            customerFlowableService.approveTask(processInstanceId);
            return ResponseData.success("审批通过成功");
        } catch (Exception e) {
            return ResponseData.error("审批通过失败: " + e.getMessage());
        }
    }
    
    /**
     * 拒绝审批
     */
    @PostMapping("/tasks/reject")
    public ResponseData<?> rejectTask(@RequestParam("processInstanceId") String processInstanceId, @RequestParam("reason") String reason) {
        try {
            customerFlowableService.rejectTask(processInstanceId, reason);
            return ResponseData.success("审批拒绝成功");
        } catch (Exception e) {
            return ResponseData.error("审批拒绝失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询流程状态
     */
    @GetMapping("/process/status")
    public ResponseData<?> getProcessStatus(@RequestParam String processInstanceId) {
        try {
            Map<String, Object> status = customerFlowableService.getProcessStatus(processInstanceId);
            return ResponseData.success(status);
        } catch (Exception e) {
            return ResponseData.error("查询流程状态失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询流程步骤
     */
    @GetMapping("/process/steps")
    public ResponseData<?> getProcessSteps(@RequestParam String processInstanceId) {
        try {
            List<Map<String, Object>> steps = customerFlowableService.getProcessSteps(processInstanceId);
            return ResponseData.success(steps);
        } catch (Exception e) {
            return ResponseData.error("查询流程步骤失败: " + e.getMessage());
        }
    }
    

}